import api from "../../api/api";
export function getcampusList(num) {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("backup/area/areaTree", {})
      .then((res) => {
        if (res.data.code == 1) {
          var treelist = res.data.data;
          resolve(treelist);
        }
      })
      .catch((err) => { });
  });
}
export function getDefault(treelist) {
  var treeExpandData = [];
  if (treelist && treelist.length > 0) {
    treeExpandData.push(treelist[0].areaCode);
    if (treelist[0].child && treelist[0].child.length > 0) {
      treeExpandData.push(treelist[0].child[0].areaCode);
    } else if (
      treelist[0].child &&
      treelist[0].child[0].child &&
      treelist[0].child[0].child.length > 0
    ) {
      treeExpandData.push(treelist[0].child[0].child[0].areaCode);
    } else if (
      treelist[0].child &&
      treelist[0].child[0].child[0].child &&
      treelist[0].child[0].child[0].child.length > 0
    ) {
      treeExpandData.push(treelist[0].child[0].child[0].child[0].areaCode);
    } else if (
      treelist[0].child &&
      treelist[0].child[0].child[0].child &&
      treelist[0].child[0].child[0].child[0].child &&
      treelist[0].child[0].child[0].child[0].child.length > 0
    ) {
      treeExpandData.push(
        treelist[0].child[0].child[0].child[0].child[0].areaCode
      );
    }
    return treeExpandData;
  }
}
export function branchOfficeTree() {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("backup/area/branchOfficeTree", {})
      .then((res) => {
        if (res.data.code == 1) {
          var treelist = res.data.data;
          resolve(treelist);
        }
      })
      .catch((err) => { });
  });
}
export function listExist(parentCode) {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("backup/area/listExist", { parentCode: parentCode })
      .then((res) => {
        if (res.data.code == 1) {
          var treelist = res.data.data;
          resolve(treelist);
        }
      })
      .catch((err) => { });
  });
}
export function campusAlllistExist(parentCode) {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("backup/area/listAreaNameByAreaCode", { areaCode: parentCode })
      .then((res) => {
        if (res.data.code == 1) {
          var treelist = res.data.data;
          resolve(treelist);
        }
      })
      .catch((err) => { });
  });
}
export function getAeaTree() {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("backup/area/areaTree", {})
      .then((res) => {
        var school_list = [];
        var treelist = res.data.data;
        bbq: for (var i = 0; i < treelist.length; i++) {
          if (treelist[i].areaCode.length == 10) {
            school_list.push(treelist[i]);
          } else {
            ccc: for (var j = 0; j < treelist[i].child.length; j++) {
              ddd: for (var z = 0; z < treelist[i].child[j].child.length; z++) {
                if (treelist[i].areaCode.length == 6) {
                  school_list.push(treelist[i]);
                  break ccc;
                  break ddd;
                } else if (treelist[i].child[j].areaCode.length == 6) {
                  school_list.push(treelist[i].child[j]);
                  break ddd;
                } else if (treelist[i].child[j].child[z].areaCode.length == 6) {
                  school_list.push(treelist[i].child[j].child[z]);
                }
              }
            }
          }
        }
        resolve(school_list);
      })
      .catch((err) => { });
  });
}
export function getAeaTreeList() {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("backup/area/areaTree", {})
      .then((res) => {
        var treelist = res.data.data;
        resolve(treelist);
      })
      .catch((err) => { });
  });
}
export function getinfluxData(field, devType, shunt, imei, startTime, endTime) {
  return new Promise(function (resolve, reject) {
    api
      .postFormAPI("backup/deviceData/influxData", {
        devType: Number(devType),
        shunt: Number(shunt),
        imei: imei,
        field: field,
        startTime: startTime,
        endTime: endTime,
        pageSize: 6000,
      })
      .then((res) => {
        if (res.data.code == 1) {
          var list = res.data.data;
          resolve(list);
        }
      })
      .catch((err) => { });
  });
}
export function DateMinus(date1, date2) {
  var sdate = new Date(date1);
  var now = new Date(date2);
  var days = now.getTime() - sdate.getTime();
  var day = parseInt(days / (1000 * 60 * 60 * 24));
  return day;
}
